package com.oa.co.controller;
/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 */

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.oa.co.bo.ScheduleBo;
import com.oa.co.common.PageVo;
import com.oa.co.common.R;
import com.oa.co.pojo.Schedule;
import com.oa.co.pojo.User;
import com.oa.co.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author sunXuDong
 * @data 2023/5/8
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/api/schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    //分页查询所有数据
    @GetMapping ("/queryPage")
    public PageVo queryPage(Integer page, Integer limit, String d_id,Integer d_code,String applicant){
        return scheduleService.queryPage(page,limit,d_id,d_code,applicant);
    }
    //柱图
    @RequestMapping ("/bar")
    public R bar(User user){

        return scheduleService.bar(user);
    }
    //饼图
    @RequestMapping ("/pie")
    public R pie(User user){

        return scheduleService.pie(user);
    }
    //审批修改状态
    @GetMapping("eaa")
    public R eaa(String d_id,Integer d_code){
    return scheduleService.eaa(d_id,d_code);
    }
    //下载文件
    @RequestMapping("/export")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response, Integer page, Integer limit,String d_id,Integer d_code,String applicant) throws IOException {
        //下载 excel  模板文件
        //设置下载excel 的 响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        //防止中文乱码
        String filename = URLEncoder.encode("个人申请", "utf-8");

        //告诉浏览器  你要下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到一个 指向浏览器的输出流
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, ScheduleBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        //准备列表数据
        PageInfo<ScheduleBo> pageData = scheduleService.scheduleBo(page,limit,d_id,d_code,applicant);
//        List<Schedule> data = pageData.getData();
        System.out.println(pageData.getList());
        sheet.doWrite(pageData.getList());
    }
}
